<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            display: block;
            margin-top: 20px;
            border-radius: 10px;
            margin-left: 600px;
            width: 300px;
            height: 30px;
            border: none;
            background-color: lightgray;
            padding-left: 10px;
            box-sizing: border-box;
        }

        button {
            display: block;
            margin-top: 20px;
            border-radius: 10px;
            margin-left: 600px;
            width: 300px;
            height: 30px;
            border: none;
            background-color: lightblue;
        }
    </style>
</head>

<body>

</body>
<input type="text" placeholder="请输入账号">
<input type="password" placeholder="请输入密码">
<input type="password" placeholder="确认密码">
<button>注册</button>

</html>
<script src="./ajax.js"></script>
<script>
    // function Search(obj) {
    //     var str = ``
    //     for (const key in obj) {
    //         str +=`${key}=${obj[key]}&`
    //     }
    //     return str.slice(0,str.length-1)
    // }
    // function ajax(method,url,params,cd){
    //     //创建请求对象
    //     var xhr = new XMLHttpRequest()
    //     //利用open与服务器建立连接
    //     xhr.open(method,url+'?'+Search(params))
    //     //设置回调函数
    //     xhr.onreadystatechange = function(){
    //         if (xhr.readyState==4) {
    //             cd(JSON.parse(xhr.responseText))
    //         }
    //     }
    //     xhr.send()
    // }


    var btn = document.querySelector('button')
    var inEle = document.querySelectorAll('input')
    btn.onclick = function () {
        if (inEle[0].value && inEle[1].value && inEle[2].value) {
            if (/^\d{4}$/.test(inEle[0].value) || /^\d{4}$/.test(inEle[1].value) || /^\d{4}$/.test(inEle[2].value)) {
                if (inEle[1].value==inEle[2].value) {
                    ajax('get','https://yantianfeng.com/api/register',{
                        username:inEle[0].value,
                        password:inEle[1].value,
                        password2:inEle[2].value
                    },function(data){
                        console.log(data);
                    })
                }else {
                    alert('两次密码不一致')
                }
            }else{
                alert('请输入正确的密码/账号')
            }
        } else {
            alert('账号密码不能为空')
        }
    }
</script>